import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import Layout from "@/layout/index.vue";
import { useAuthStore } from "@/stores/auth.js";
import NProgress from "nprogress";

// 静态路由
export const staticRouters = [
  // {
  //   path: "/",
  //   redirect: "/login",
  // },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      title: "系统登录",
    },
  },
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },
  {
    name: "layout",
    path: "",
    component: Layout,
    children: [
      // {
      //   path: "",
      //   name: "home",
      //   children: [
      //     {
      //       path: "",
      //       name: "homeIndex",
      //       component: () => import("@/views/home/index.vue"),
      //       meta: {
      //         title: "首页",
      //         cache: true,
      //       },
      //     },
      //     {
      //       path: "details",
      //       name: "homedetails",
      //       component: () => import("@/views/home/details/index.vue"),
      //       meta: {
      //         title: "首页详情",
      //         cache: true,
      //       },
      //     },
      //   ],
      // },
      // {
      //   path: "dashboard",
      //   name: "dashboard",
      //   component: () => import("@/views/dashboard/index.vue"),
      //   meta: {
      //     title: "dashboard",
      //     cache: true,
      //   },
      // },
      // {
      //   path: "demo",
      //   name: "demo",
      //   component: () => import("@/views/demo/index.vue"),
      //   meta: {
      //     title: "demo",
      //     cache: true,
      //   },
      // },
      // 将匹配所有内容并将其放在 `$route.params.pathMatch` 下
      {
        path: "/:pathMatch(.*)*",
        name: "NotFound",
        component: () => import("@/views/NotFond.vue"),
        meta: {
          title: "Not Found",
          icon: "local|/src/assets/icon/404.svg",
        },
      },
    ],
  },
];
//前端配置的动态路由 menu是菜单 dir是目录
export const dynamicRoutes = [
  {
    id: 3,
    parentId: null,
    title: "中心简介",
    name: "introduction-center",
    path: "introduction-center",
    component: "/src/views/introduction-center/index.vue",
    platform: "web",
    type: "menu",
    handleType: "route",
    icon: "history",
    cache: false,
    enabled: true,
  },
  {
    id: 4,
    parentId: null,
    title: "办事指南",
    name: "service-guide",
    path: "service-guide",
    platform: "web",
    type: "dir",
    handleType: "route",
    icon: "alarm",
    order: 3,
    cache: false,
    enabled: true,
  },
  // {
  //   id: 41,
  //   parentId: 4,
  //   title: "办事指南-首页",
  //   name: "service-guide-home",
  //   path: "",
  //   component: "/src/views/service-guide/index.vue",
  //   platform: "web",
  //   type: "menu",
  //   handleType: "route",
  //   outerUrl: null,
  //   cache: false,
  //   enabled: true,
  //   hidden: true,
  // },
  {
    id: 42,
    parentId: 4,
    title: "demo",
    name: "demo",
    path: "demo",
    component: "/src/views/demo/index.vue",
    platform: "web",
    type: "menu",
    handleType: "route",
    outerUrl: null,
    cache: false,
    enabled: true,
  },
  {
    id: 43,
    parentId: 4,
    title: "demo1",
    name: "demo1",
    path: "demo1",
    component: "/src/views/demo1/index.vue",
    platform: "web",
    type: "menu",
    handleType: "route",
    outerUrl: null,
    cache: false,
    enabled: true,
  },
  // {
  //   id: 42,
  //   parentId: 4,
  //   title: "办事指南-详情",
  //   name: "service-guide-details",
  //   path: "service-guide-details",
  //   component: "/src/views/service-guide/details/index.vue",
  //   platform: "web",
  //   type: "menu",
  //   handleType: "route",
  //   outerUrl: null,
  //   cache: false,
  //   enabled: true,
  //   hidden: true,
  // },
  {
    id: 1,
    parentId: null,
    title: "地图导航",
    name: "map-navigation",
    path: "map-navigation",
    component: "/src/views/map-navigation/index.vue",
    platform: "web",
    type: "menu",
    handleType: "route",
    icon: "moon",
    cache: false,
    enabled: true,
  },
  {
    id: 2,
    parentId: null,
    title: "制度公告",
    name: "system-announcement",
    path: "system-announcement",
    platform: "web",
    type: "dir",
    handleType: "route",
    icon: "password",
    order: 2,
    cache: false,
    enabled: true,
    isHidden: true,
  },
  {
    id: 21,
    parentId: 2,
    title: "办事指南-首页",
    name: "system-announcement-home",
    path: "",
    component: "/src/views/system-announcement/index.vue",
    platform: "web",
    type: "menu",
    handleType: "route",
    outerUrl: null,
    cache: false,
    enabled: true,
    hidden: true,
  },
  // {
  //   id: 22,
  //   parentId: 2,
  //   title: "办事指南-详情",
  //   name: "system-announcement-details",
  //   path: "system-announcement-details",
  //   component: "/src/views/system-announcement/details/index.vue",
  //   platform: "web",
  //   type: "menu",
  //   handleType: "route",
  //   outerUrl: null,
  //   cache: false,
  //   enabled: true,
  //   hidden: true,
  // },
  // {
  //   id: 5,
  //   createTime: '2023-03-05 21:55:27',
  //   updateTime: '2025-01-07 15:30:55',
  //   createBy: 1,
  //   updateBy: 3,
  //   roleId: 1,
  //   parentId: null,
  //   title: '导出记录',
  //   name: 'exportRecords',
  //   path: 'exportRecords',
  //   component: '/src/views/exportRecords/index.vue',
  //   platform: 'web',
  //   type: 'menu',
  //   handleType: 'route',
  //   outerUrl: null,
  //   icon: 'local|/src/assets/icon/search-list.svg',
  //   order: 3,
  //   cache: false,
  //   enabled: true,
  // },
  // {
  //   id: 7,
  //   createTime: '2023-03-05 21:55:27',
  //   updateTime: '2025-01-07 15:30:55',
  //   createBy: 1,
  //   updateBy: 3,
  //   roleId: 1,
  //   parentId: null,
  //   title: '部门管理',
  //   name: 'department',
  //   path: 'department',
  //   component: '/src/views/department/index.vue',
  //   platform: 'web',
  //   type: 'menu',
  //   handleType: 'route',
  //   outerUrl: null,
  //   icon: 'local|/src/assets/icon/department.svg',
  //   order: 3,
  //   cache: false,
  //   enabled: true,
  // },
  // {
  //   id: 6,
  //   createTime: '2023-03-05 21:55:27',
  //   updateTime: '2025-01-07 15:30:55',
  //   createBy: 1,
  //   updateBy: 3,
  //   roleId: 1,
  //   parentId: null,
  //   title: '用户管理',
  //   name: 'role',
  //   path: 'role',
  //   component: '/src/views/role/index.vue',
  //   platform: 'web',
  //   type: 'menu',
  //   handleType: 'route',
  //   outerUrl: null,
  //   icon: 'local|/src/assets/icon/user-group.svg',
  //   order: 3,
  //   cache: false,
  //   enabled: true,
  // },
  // {
  //   id: 8,
  //   createTime: '2023-03-05 21:55:27',
  //   updateTime: '2025-01-07 15:30:55',
  //   createBy: 1,
  //   updateBy: 3,
  //   roleId: 1,
  //   parentId: null,
  //   title: '组织管理',
  //   name: 'organization',
  //   path: 'organization',
  //   component: '/src/views/organization/index.vue',
  //   platform: 'web',
  //   type: 'menu',
  //   handleType: 'route',
  //   outerUrl: null,
  //   icon: 'local|/src/assets/icon/organization.svg',
  //   order: 3,
  //   cache: false,
  //   enabled: true,
  // },
  // {
  //   id: 6,
  //   createTime: '2023-03-05 21:55:27',
  //   updateTime: '2024-05-05 19:20:37',
  //   createBy: 1,
  //   updateBy: 1,
  //   roleId: 1,
  //   parentId: null,
  //   title: '测试菜单',
  //   name: 'test',
  //   path: 'test',
  //   component: null,
  //   platform: 'web',
  //   type: 'dir',
  //   handleType: null,
  //   outerUrl: null,
  //   icon: 'local|/src/assets/icon/wechat.svg',
  //   order: 10,
  //   cache: true,
  //   enabled: true,
  // },
  // {
  //   id: 7,
  //   createTime: '2023-03-05 21:55:27',
  //   updateTime: '2024-05-05 19:20:37',
  //   createBy: 1,
  //   updateBy: 1,
  //   roleId: 1,
  //   parentId: 6,
  //   title: '测试菜单1111',
  //   name: 'demo',
  //   path: 'demo',
  //   component: '/src/views/demo/index.vue',
  //   platform: 'web',
  //   type: 'menu',
  //   handleType: 'route',
  //   outerUrl: null,
  //   icon: 'local|/src/assets/icon/home.svg',
  //   order: 1,
  //   cache: true,
  //   enabled: true,
  // },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes: staticRouters,
});

// router.beforeEach(async (to, from, next) => {
//   NProgress.configure({ showSpinner: false });
//   NProgress.start();
//   next();
// });

router.beforeEach(async (to, from, next) => {
  NProgress.configure({ showSpinner: false });
  NProgress.start();
  const authStore = useAuthStore();

  const path = await authStore.beforeEach(to);
  if (path) next(path);
  else next();
});

router.afterEach((to) => {
  NProgress.done();
});

export default router;
